package com.paireach.szwolaiyun.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.paireach.szwolaiyun.R;
import com.paireach.szwolaiyun.adapter.WaybillDetailsAdapter;
import com.paireach.szwolaiyun.mode.DispatchMode;
import com.paireach.szwolaiyun.mode.GoodsMode;
import com.paireach.szwolaiyun.util.MessageInfoUtil;
import com.paireach.szwolaiyun.util.MyHttpUtil;
import com.paireach.szwolaiyun.util.StringUtil;

/**
 * Created by Administrator on 2016/6/21.
 */
public class WaybillDetailsFragment extends Fragment implements View.OnClickListener {
    private View view;
    private Handler handler;
    private Button btn_acceptOrder;
    private DispatchMode dispatchMode;
    private GoodsMode goodsMode;

    private WaybillDetailsAdapter waybillDetailsAdapter;

    private ListView lv_orderdetail;//产品列表


    private TextView tv_home_ordercode;//运单号
    private TextView tv_ordertime;//装运日期
    private TextView tv_originname;//装货工厂
    private TextView tv_origin_address;//装货工厂地址
    private TextView tv_endname;//收货工厂
    private TextView tv_end_address;//收货工厂地址


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_waybill_details,null);
        initControl();
        MyHttpUtil.getGoodsDetails(dispatchMode.getCode(),httpHandler);
        return view;
    }

    public void initHandle(Handler handler){
    this.handler=handler;
    }
    private void initControl(){

        lv_orderdetail=(ListView)view.findViewById(R.id.lv_orderdetail);
        tv_home_ordercode=(TextView)view.findViewById(R.id.tv_home_ordercode);
        tv_ordertime=(TextView)view.findViewById(R.id.tv_ordertime);
        tv_originname=(TextView)view.findViewById(R.id.tv_originname);
        tv_origin_address=(TextView)view.findViewById(R.id.tv_origin_address);
        tv_endname=(TextView)view.findViewById(R.id.tv_endname);
        tv_end_address=(TextView)view.findViewById(R.id.tv_end_address);

        btn_acceptOrder=(Button)view.findViewById(R.id.btn_acceptOrder);
        btn_acceptOrder.setOnClickListener(this);

        tv_home_ordercode.setText(dispatchMode.getCode());
        tv_ordertime.setText(dispatchMode.getDate());
        tv_originname.setText(dispatchMode.getDeliveryPlant());
        tv_origin_address.setText(dispatchMode.getDeliveryPlantAddress());
        tv_endname.setText(dispatchMode.getReceivingPlant());
        tv_end_address.setText(dispatchMode.getReceivingPlantAddress());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_acceptOrder:
                if(dispatchMode.getStatusCode().equals(StringUtil.NOT_RECEIVE)){
                    MyHttpUtil.acceptOrder(dispatchMode.getId(),httpHandler);
                }else{
                    Message message=new Message();
                    message.what=MessageInfoUtil.WAYBILL_TO_SGIN;
                    message.obj=dispatchMode;
                    handler.sendMessage(message);
                }


                break;
        }
    }
    private Handler httpHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case MessageInfoUtil.GET_DISPATCH_SUCCEED:
                    goodsMode=new GoodsMode();
                    goodsMode.initData(msg.obj.toString());
                    waybillDetailsAdapter=new WaybillDetailsAdapter(getActivity(),goodsMode);
                    lv_orderdetail.setAdapter(waybillDetailsAdapter);
                    break;
                case MessageInfoUtil.GET_DISPATCH_FAIL:

                    break;
                case MessageInfoUtil.ACCEPT_ORDER_SUCCEED:
                    Message message=new Message();
                    message.what=MessageInfoUtil.WAYBILL_TO_SGIN;
                    dispatchMode.setStatusCode(StringUtil.GO_TO_SHIPMENT);
                    dispatchMode.setStatusName("装货在途");
                    message.obj=dispatchMode;
                    handler.sendMessage(message);

                    Log.e("0011","WaybillDetailsFragment接收运单的消息"+msg.obj.toString());

                    break;
                case MessageInfoUtil.ACCEPT_ORDER_FAIL:
                    Log.e("0011","WaybillDetailsFragment接收运单失败的消息"+msg.obj.toString());
                    break;
            }
        }
    };

    //由Activity传入运单号，用来查询运单信息
    public void initData(DispatchMode mode){
//        Log.e("0011","接收到运单号"+num);
        dispatchMode=mode;
    }
}
